package com.iostream;

import java.io.*;

/**
 * @Author WaveHi
 * @Date 20/8/2021 下午 3:40
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description Test the use of java.io.DataInputStream anf java.io.DataOutputStream
 */
public class TestDataStream {

    public static void main(String[] args) {

        //获取数据
        int num1 = 10;
        int num2 = 23;
        DataOutputStream dos1 = null;
        DataInputStream dis1 = null;
        DataOutputStream dos2 = null;

        try {
            //获取目标位置
            File destFile = new File("day11/data/data.dat");
            File srcFile = new File("day11/data/data2.dat");

            //获取流
            FileOutputStream fos1 = new FileOutputStream(srcFile);
            FileInputStream fis1 = new FileInputStream(srcFile);
            FileOutputStream fos2 = new FileOutputStream(destFile);
            dos1 = new DataOutputStream(fos1);
            dis1 = new DataInputStream(fis1);
            dos2 = new DataOutputStream(fos2);

            //数据的存取
            dos1.writeBoolean(false);
            dos1.writeUTF("Wow!");
            dos1.flush();
            boolean b1 = dis1.readBoolean();
            String s1 = dis1.readUTF();
            System.out.println(b1);
            System.out.println(s1);
            dos2.writeBoolean(b1);
            dos2.writeUTF(s1);
            dos2.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //资源的关闭
            if(dis1 != null) {

                try {
                    dis1.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            if(dos1 != null) {

                try {
                    dos1.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            if(dos2 != null) {

                try {
                    dos2.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }


    }

}
